package cz.cvut.fit.web.nakupniSeznam.ws.conv;

import java.net.URI;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

import cz.cvut.fit.web.nakupniSeznam.data.Item;
import cz.cvut.fit.web.nakupniSeznam.data.List;

@XmlRootElement(name = "list")
@XmlType(propOrder = { "id","mobileId", "name", "items", "lastChanged", "uri" })
public class ListConverter {

	public ListConverter() {
		this.list = new List();
		this.mobileId = null;
		this.expand = true;

	}

	public ListConverter(List list, URI uri) {
		super();

		this.list = list;
		this.mobileId = null;
		this.expand = false;
		this.uri = repairUri(uri);
	}

	public ListConverter(List list, java.util.List<Item> items, URI uri) {
		super();
		this.list = list;
		this.mobileId = null;
		this.expand = true;
		this.uri = repairUri(uri);

		this.items = new ItemsConverter(items, uri);
	}

	private URI repairUri(URI uri) {
		String s = uri.getPath().replaceFirst("/list.*", "/list/");
		URI listUri = URI.create(s + this.list.getId() + "/detail");
		URI resolvedUri = uri.resolve(listUri);
		return resolvedUri;
	}

	private URI uri;

	private boolean expand;

	private List list;
	private Integer mobileId;

	private ItemsConverter items;

	@XmlElement
	public ItemsConverter getItems() {
		if (expand == true) {
			return items;
		} else
			return null;
	}

	@XmlElement
	public Long getId() {
		return this.list.getId();
	}

	@XmlElement
	public String getName() {
		if (expand == true) {
			return this.list.getName();
			// return name;
		} else
			return null;
	}

	@XmlElement
	public Long getLastChanged() {
		return this.list.getLastChanged();
		// return lastChanged;
	}

	@XmlElement
	public URI getUri() {
		if (expand == true) {
			return null;
		} else
			return uri;
	}

	public void setId(Long id) {
		this.list.setId(id);
	}

	public void setName(String name) {
		this.list.setName(name);
	}

	public void setLastChanged(Long lastChanged) {
		this.list.setLastChanged(lastChanged);
	}

	public List retrieveRepresentation() {
		return list;
	}

	public void setItems(ItemsConverter items) {
		this.items = items;
	}

	@XmlElement
	public Integer getMobileId() {
		return mobileId;
	}

	public void setMobileId(Integer mobileId) {
		this.mobileId = mobileId;
	}
}
